package com.library.admin.controller;

import com.library.admin.modules.rolemenu.vo.RoleMenuVO;
import com.library.admin.modules.rolemenu.bo.RoleMenuPage;
import com.library.admin.modules.rolemenu.bo.RoleMenuInsert;
import com.library.admin.modules.rolemenu.bo.RoleMenuUpdate;
import com.library.admin.modules.rolemenu.service.RoleMenuService;
import com.library.logging.annotation.LogSys;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.library.common.response.Result;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;

import java.util.Objects;


/**
 * @Author xyh
 * @Desc 角色和菜单关联表(RoleMenu)表控制层
 * @Date 2023-10-02 15:09:33
 */

@RestController
@RequestMapping("rolemenu")
public class RoleMenuController {

    @Resource
    private RoleMenuService roleMenuService;


    /**
     * 分页查询列表
     *
     * @return 数据
     */
    @LogSys(value = "角色和菜单分页查询列表")
    @GetMapping("/list")
    public Result<IPage<RoleMenuVO>> queryByPage(@Valid RoleMenuPage page) {
        return Result.success(roleMenuService.queryByPage(page));
    }

    /**
     * 根据ID获取数据
     *
     * @return 单个角色和菜单关联表数据
     */
    @LogSys(value = "单个角色和菜单关联表数据")
    @PostMapping("/queryById/{id}")
    public Result<RoleMenuVO> queryById(@PathVariable("id") Integer id) {
        return Result.success(roleMenuService.queryById(id));
    }


    /**
     * 添加角色和菜单关联表
     *
     * @return 新增角色和菜单关联表数据
     */
    @LogSys(value = "新增角色和菜单关联表数据")
    @PostMapping("/insert/{roleId}")
    public Result insert(@PathVariable Integer roleId, @Valid @RequestBody RoleMenuInsert param) {
        param.setRoleId(roleId);
        roleMenuService.insert(param);
        return Result.success();
    }


    /**
     * 编辑角色和菜单关联表
     *
     * @return 编辑角色和菜单关联表数据
     */
    @LogSys(value = "编辑角色和菜单关联表数据")
    @PutMapping("/update")
    public Result update(@Valid @RequestBody RoleMenuUpdate param) {
        if (Objects.isNull(param.getId())) {
            return Result.error("ID不能为空");
        }
        roleMenuService.update(param);
        return Result.success();
    }

    /**
     * 删除角色和菜单关联表
     *
     * @return 删除角色和菜单关联表数据
     */
    @LogSys(value = "删除角色和菜单关联表数据")
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable("id") Integer id) {
        roleMenuService.deleteById(id);
        return Result.success();
    }
}
